/*
 * File Name：CalendarViewAdapter.java
 * Copyright：Copyright 2008-2015 CiWong.Inc. All Rights Reserved.
 * Description： CalendarViewAdapter.java
 * Modify By：pla-gysu
 * Modify Date：2015年3月30日
 * Modify Type：Add
 */
package com.ciwong.epaper.modules.me.adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.ciwong.mobilelib.widget.NoScrollGridView;
import com.ciwong.epaper.R;
import com.ciwong.epaper.modules.me.bean.WrongQuestion;
import com.ciwong.epaper.modules.me.ui.MyWrongQuesActivity;

import java.util.List;

/**
 * 日历适配器
 *
 * @author pla-gysu
 * @version ciwong v.1.0 2015年3月30日
 * @since ciwong v.1.0
 */
public class WrongQuesPagerAdapter extends PagerAdapter {

    private List<WrongQuestion> mQuesList;

    /**
     * 每个gridview内item最大个数
     */
    private static final int ITEM_GRIDVIEW_COUNT = 10;

    private Context mContext;

    private WrongQuesAdapter oldAdapter;

    private int mCurrTotalIndex;// 当前总下标

    public WrongQuesPagerAdapter(Context context, List<WrongQuestion> mQuesList) {
        super();
        this.mQuesList = mQuesList;
        this.mContext = context;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {

        int pageSize = getCount();

        for (int pageIndex = 0; pageIndex < pageSize; pageIndex++) {
            NoScrollGridView gv = (NoScrollGridView) View.inflate(mContext,
                    R.layout.item_wrong_ques_gv, null);

            final int startIndex = pageIndex * ITEM_GRIDVIEW_COUNT;
            int endIndex = startIndex + ITEM_GRIDVIEW_COUNT;
            List<WrongQuestion> itemQuesList = mQuesList.subList(startIndex,
                    endIndex > mQuesList.size() ? mQuesList.size() : endIndex);
            final WrongQuesAdapter mAdapter = new WrongQuesAdapter(mContext,
                    itemQuesList);
            mAdapter.setmCurrQuesIndex(pageIndex == 0 ? 0 : -1);
            gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                                        int position, long id) {
                    int totalIndex = position + startIndex;
                    if (mCurrTotalIndex != totalIndex) {
                        if (oldAdapter != null) {
                            oldAdapter.setmCurrQuesIndex(-1);
                            oldAdapter.notifyDataSetChanged();
                        }
                        mAdapter.setmCurrQuesIndex(position);
                        mAdapter.notifyDataSetChanged();
                        oldAdapter = mAdapter;
                        // 刷新数据显示
                        if (mContext instanceof MyWrongQuesActivity) {
                            MyWrongQuesActivity activity = (MyWrongQuesActivity) mContext;
                            activity.refreshQuestionShow(totalIndex);
                            mCurrTotalIndex = totalIndex;
                        }
                    }
                }
            });
            gv.setAdapter(mAdapter);
            ((ViewPager) container).addView(gv, pageIndex);
        }

        return ((ViewPager) container).getChildAt(position);
    }

    @Override
    public int getCount() {
        if (mQuesList != null && !mQuesList.isEmpty()) {
            int size = mQuesList.size();
            int pageSize = size / ITEM_GRIDVIEW_COUNT;
            return size % ITEM_GRIDVIEW_COUNT == 0 ? pageSize : pageSize + 1;
        }
        return 0;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        ((ViewPager) container)
                .removeView((View) container.getChildAt(position));
    }

}
